package zj.com.activities;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import zj.com.adapter.GsSelectAdapter;
import zj.com.adapter.XmSelectAdapter;
import zj.com.model.Org;
import zj.com.model.Project;
import zj.com.net.NetManager;
import zj.com.net.OnResponseListener;
import zj.com.zj.R;

/**
 * Created by xi on 16/6/29.
 */
public class XmSelectActivity extends Activity implements View.OnClickListener{

    public ListView listView;
    public TextView title_name;
    public ImageView left_iv;

    public List<Project> list;
    public XmSelectAdapter adapter;

    public TextView right_tv;
    public  boolean isMuti;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_gs_select);
        left_iv = (ImageView) findViewById(R.id.left_iv);
        left_iv.setOnClickListener(this);
        left_iv.setVisibility(View.VISIBLE);


        listView = (ListView) findViewById(R.id.listView);
        title_name = (TextView) findViewById(R.id.title_name);
        title_name.setText("选择");
        list = new ArrayList<>();
        adapter =new XmSelectAdapter(list,this);
        listView.setAdapter(adapter);
        right_tv = (TextView) findViewById(R.id.right_tv);
        right_tv.setVisibility(View.VISIBLE);
        right_tv.setText("确定");
        right_tv.setOnClickListener(this);

         isMuti = getIntent().getBooleanExtra("isMuti",true);
        if(isMuti){
            right_tv.setVisibility(View.VISIBLE);
        }else{
            right_tv.setVisibility(View.GONE);

        }

        int type = getIntent().getIntExtra("type",0);


            Org gs = (Org) getIntent().getSerializableExtra("org");

            if(gs == null){

                Toast.makeText(this, "请先选择项目", Toast.LENGTH_SHORT).show();
            }else{

                NetManager.getInstance(this).GetProjectList(gs.getOrgCode(), new OnResponseListener<List<Project>>() {
                    @Override
                    public void onComplete(boolean isOK, List<Project> result, int errorCode, String errorMsg) {

                        if (isOK) {


                            list= result;
                            adapter.setDataChange(list);
                        } else {
                            Toast.makeText(XmSelectActivity.this, "获取失败", Toast.LENGTH_SHORT).show();

                        }
                    }

                    @Override
                    public void onInternError(int errorCode, String errorMessage) {
                        Toast.makeText(XmSelectActivity.this, "获取失败", Toast.LENGTH_SHORT).show();

                    }
                });
            }


        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                adapter.setSelect(position);

                if(!isMuti){
                    Project project = list.get(position);
                    Intent intent = new Intent();
                    intent.putExtra("project",project);
                    setResult(RESULT_OK,intent);
                    finish();
                }

            }
        });

    }



    @Override
    public void onClick(View v) {

        switch (v.getId()){
            case R.id.left_iv:
                finish();
                break;
            case R.id.right_tv:
                doSelect();
                break;
        }

    }

    public void doSelect(){
        Map<Integer,Project> map =   adapter.getSelectMap();
        List<Project> list = new ArrayList<>();
        for(Map.Entry<Integer, Project> entry:map.entrySet()){

            Project project  = entry.getValue();
            list.add(project);
        }

        Intent intent = new Intent();
        intent.putExtra("project",(Serializable)list);
        setResult(RESULT_OK,intent);
        finish();
    }
}
